package com.healthcloud.service.impl;

import com.healthcloud.dao.AreaCodeMapper;
import com.healthcloud.dao.LargeAreaMapper;
import com.healthcloud.entity.AreaCode;
import com.healthcloud.entity.Condition;
import com.healthcloud.entity.LargeArea;
import com.healthcloud.service.LargeAreaService;
import com.healthcloud.utils.Pagine;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by xinxibu on 2015/7/8.
 */
@Transactional
@Service
public class LargeAreaServiceImpl implements LargeAreaService {

    @Autowired
    private LargeAreaMapper largeAreaMapper;

    @Autowired
    private AreaCodeMapper areaCodeMapper;


    @Override
    public LargeArea get(String largeAreaCode) {
        return largeAreaMapper.get(largeAreaCode);
    }

    @Override
    public LargeArea getByLargeAreaName(String largeAreaName) {
        return largeAreaMapper.getByLargeAreaName(largeAreaName);
    }

    @Override
    public List<LargeArea> findByEntity(LargeArea entity, Pagine<LargeArea> pagine) {
        return largeAreaMapper.findByEntity(entity, pagine);
    }

    @Override
    public List<LargeArea> findByEntityAll(LargeArea entity) {
        return largeAreaMapper.findByEntityAll(entity);
    }

    @Override
    public List<LargeArea> criteriaSearch(List<Condition<Object>> conditions, Pagine<LargeArea> pagine) {
        return largeAreaMapper.criteriaSearch(conditions, pagine);
    }

    @Override
    public int save(LargeArea entity) {
        return largeAreaMapper.save(entity);
    }

    @Override
    public int update(LargeArea entity, String largeAreaCodeOld) {
        AreaCode areaCode = new AreaCode();
        areaCode.setUpdatedTime(new Date());
        areaCode.setLargeAreaCode(entity.getLargeAreaCode());
        areaCodeMapper.updateLargeAreaCode(largeAreaCodeOld, areaCode);
        return largeAreaMapper.updateByOldId(entity, largeAreaCodeOld);
    }

    @Override
    @Transactional
    public int delete(String largeAreaCode) {
        AreaCode areaCode = new AreaCode();
        areaCode.setUpdatedTime(new Date());
        areaCodeMapper.updateLargeAreaCode(largeAreaCode,areaCode);
        return largeAreaMapper.delete(largeAreaCode);
    }

    @Override
    @Transactional
    public int deletes(String[] largeAreaCodes) {
        int j = 0;
        for (int i=0; i<largeAreaCodes.length; i++){
            j = delete(largeAreaCodes[i]);
        }
        return j;
    }
}
